package com.example.administrator.bluetoothdemo01.view;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Lzz on 8/5/2017.
 */

public class DeviceAdapter extends BaseAdapter{

    private List<BluetoothDevice> mData;
    private Context mContext;

    public DeviceAdapter(List<BluetoothDevice> data, Context context) {
        mData = data;
        mContext = context;
    }

    @Override
    public int getCount() {
        if (mData!=null){
            return mData.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (mData!=null) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView==null) {
            itemView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_expandable_list_item_2, parent, false);
        }
        TextView line1 = (TextView) itemView.findViewById(android.R.id.text1);
        TextView line2 = (TextView) itemView.findViewById(android.R.id.text2);


        BluetoothDevice device = mData.get(position);

        line1.setText(device.getName());
        line2.setText(device.getAddress());

        return itemView;
    }


    // 刷新 列表 (观察者模式？？？？？？？)
    public void refreshListView(List<BluetoothDevice> data){
        mData = data;
        notifyDataSetChanged();
    }

}
